



#include <WProgram.h>
#include <LinkedList.h>

//Hint: A link is defined as a structure with 2 fields: value and a pointer to the next link.
//Hint: A linked list is defined as a structure with 2 fields. Number of links in the list and a pointer to the first link.

/* The function adds the referenced value to 
the referenced list. */

LinkedList::LinkedList(){
}


void LinkedList::add2list(int *value, linkedlist *list) {    
  //- Creat a new link
  //- Give the link all its necessary values.
  link L = {*value, list -> first}; 
  //- Update the appropriate fields in the linked list.
  list -> first = &L; //Newly added link is now the first link in the list
  (list -> size)++;   //Size of the list increased.	
}

/* The function returns the next/first value 
in the referenced linked list */
int LinkedList::getnext(linkedlist *list) {
  //- Create a new integer. 
  int val;
  
  //- Give the integer value.
  val  = (list -> first) -> value; //Reference to the value of the first link in the list
  
  //- Update the appropriate fields in the linked list
  list -> first = (list -> first) -> next; //The second link is now the first link in the updated list
  (list -> size)--;  //Size of the list decreased
  return val;
}


/* The function takes a String of values (comma separated) and
returns them in a linked list. */
linkedlist LinkedList::parsestring(char *(s[])){
  //- Created a new linked list;
  linkedlist list;
  
  //- Give the list its initial values.
  list.size = 0;
  
  //- Analyze the string.
  int buffer = 0;   //the integer "buffer" acts as a temporary variable to store the value before it is loaded to the linked list
  for(int i = 0; i<sizeof(*s); i++){
    if (*s[i] == ','){     //COND: if a comma is hit -> A significant value has been extracted from a character array. 
      this->add2list(&buffer, &list); //Add the analyzed value to a linked list
      buffer = 0;  //Reset the buffer value;
    }
    else {                 //COND: otherwise -> keep analysing the characters.
      buffer = 10*buffer + *s[i];  
    }
  }
  return list;
}